package com.sugon.controller.device;

import com.sugon.annotation.AutoLog;
import com.sugon.entity.InspectionOptionEntity;
import com.sugon.service.InspectionOptionService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 设备巡检API
 *
 * @author YuXD
 * @date 2020-08-11 17:45
 */
@Controller
@RequestMapping("inspectionoption")
public class InspectionOptionController {
    @Autowired
    private InspectionOptionService inspectionOptionService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("inspectionoption:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<InspectionOptionEntity> inspectionOptionList = inspectionOptionService.queryList(query);
        int total = inspectionOptionService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(inspectionOptionList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("inspectionoption:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        InspectionOptionEntity inspectionOption = inspectionOptionService.queryObject(id);

        return R.ok().put("inspectionOption", inspectionOption);
    }

    /**
     * 保存
     */
    @AutoLog("巡检选项表--保存")
    @RequestMapping("/save")
    @RequiresPermissions("inspectionoption:save")
    @ResponseBody
    public R save(@RequestBody InspectionOptionEntity inspectionOption) {
        inspectionOptionService.save(inspectionOption);

        return R.ok();
    }

    /**
     * 修改
     */
    @AutoLog("巡检选项表--修改")
    @RequestMapping("/update")
    @RequiresPermissions("inspectionoption:update")
    @ResponseBody
    public R update(@RequestBody InspectionOptionEntity inspectionOption) {
        inspectionOptionService.update(inspectionOption);

        return R.ok();
    }

    /**
     * 删除
     */
    @AutoLog("巡检选项表--删除")
    @RequestMapping("/delete")
    @RequiresPermissions("inspectionoption:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        inspectionOptionService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        int total = inspectionOptionService.queryTotalById(params);

        return R.ok().put("num", total);
    }


    /**
     * 查看关联列表
     */
    @RequestMapping("/listByJoinId")
//    @RequiresPermissions("inspectionoption:list")
    @ResponseBody
    public R listByJoinId(@RequestParam Map<String, Object> params) {

        //查询列表数据
        Query query = new Query(params);

        List<InspectionOptionEntity> inspectionOptionList = inspectionOptionService.queryListById(query);
        int total = inspectionOptionService.queryTotalById(query);

        PageUtils pageUtil = new PageUtils(inspectionOptionList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    @RequestMapping("/imports")
    @ResponseBody
    public R imports(@RequestParam("file") MultipartFile file) throws Exception {

       inspectionOptionService.imports(file);

        return R.ok();
    }
}
